/**
 *  StaDiUM - State Diagram Unified Modeler
 *  Copyright (C) 2007  Stefan Bucur, Mihai Balan, Claudiu Tanase
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 *  File Information
 *
 *  Last Modified On: $Date: 2007-06-13 16:46:13 +0000 (Wed, 13 Jun 2007) $
 *  Last Modified By: $Author: stefan.bucur $
 */
package ro.pub.cs.stadium.platform;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

/**
 * @author stefan
 *
 */
public class SplashScreen implements PaintListener{
	private final int SPLASH_WIDTH = 500;
	private final int SPLASH_HEIGHT = 400;
	
	private final String SPLASH_LOCATION = "artwork/splash.png";
	
	private Display display;
	
	private Shell splashShell = null;
	
	private ProgressBar progressBar = null;
	
	Image splashImage = null;
	
	public SplashScreen(Display display) {
		this.display = display;
		
		splashShell = new Shell(SWT.ON_TOP);
		
		
		
		// Setting the layout
		FormLayout layout = new FormLayout();
		splashShell.setLayout(layout);
		
		splashShell.setSize(SPLASH_WIDTH, SPLASH_HEIGHT);
		
		// Creating the progress bar
		progressBar = new ProgressBar(splashShell, SWT.NONE);
		FormData progressData = new FormData();
		progressData.bottom = new FormAttachment(100);
		progressData.left = new FormAttachment(0);
		progressData.right = new FormAttachment(100);
		progressBar.setLayoutData(progressData);
		
		// Centering the splash screen
		Rectangle splashRect = splashShell.getBounds();
		Rectangle displayRect = display.getBounds();
		
		int x = (displayRect.width - splashRect.width) / 2;
		int y = (displayRect.height - splashRect.height) / 2;
		
		splashShell.setLocation(x, y);
		
		// Loading the image
		
		try {
			InputStream splashStream = this.getClass().getResourceAsStream(SPLASH_LOCATION);
			splashImage = new Image(display, splashStream);
			splashStream.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		
		splashShell.addPaintListener(this);
	}
	
	public void runSplash(ISplashRunnable job) {
		if (job == null)
			throw new NullPointerException("job parameter is null");
		
		final ISplashRunnable execjob = job;
		
		splashShell.open();	
		
		Thread runThread = new Thread(new Runnable() {
			public void run() {
				execjob.run(SplashScreen.this);
				
				closeSplash();
			}
		});
		
		runThread.start();
	}
	
	public void dispose() {
		splashShell.dispose();
	}

	// PaintListener Interface
	
	
	public void paintControl(PaintEvent e) {
		GC gc = e.gc;
		
		if (splashImage != null)
			gc.drawImage(splashImage, 0, 0);
		
		gc.setForeground(new Color(splashShell.getDisplay(), 255, 255, 255));
		
		gc.drawText(StadiumApplication.getVersion(), 10, 300, true);
		
		gc.drawText("Copyright (C) 2007", 10, 330, true);
		gc.drawText("Stefan Bucur, Mihai Balan, Claudiu Tanase", 10, 350, true);
	}
	
	/**
	 * Closes the splash screen in a thread safe manner
	 * 
	 */
	public void closeSplash() {
		display.asyncExec(new Runnable() {
			public void run() {
				if (splashShell.isDisposed())
					return;
				
				splashShell.close();
				
			}
			
		});
	}
	
	/**
	 * Sets the maximum progress bar value in a thread safe manner
	 * @param value
	 */
	public void setProgressMax(final int value) {
		display.asyncExec(new Runnable() {
			public void run() {
				if (progressBar.isDisposed())
					return;
				
				progressBar.setMinimum(0);
				progressBar.setMaximum(value);
			}
 		});
	}
	
	/**
	 * Sets the current progress bar value in a thread safe manner
	 * @param value
	 * @param note
	 */
	public void setProgress(final int value, final String note) {
		display.asyncExec(new Runnable() {
			public void run() {
				if (progressBar.isDisposed())
					return;
				
				progressBar.setSelection(value);
			}
		});
	}
}
